package com.yantong.service.impl;

import com.yantong.pojo.Brokerage;
import com.yantong.pojo.BusinessCardIndustry;
import com.yantong.pojo.BusinessCardIndustryExample;
import com.yantong.pojo.TBusinessCard;
import com.yantong.pojo.vo.BrokerageVo;
import com.yantong.mapper.BusinessCardIndustryMapper;
import com.yantong.mapper.TBusinessCardMapper;
import com.yantong.service.TBusinessCardService;
import com.yantong.utils.PageUtils;
import com.yantong.constant.CommissionLogType;
import com.yantong.pojo.CommissionLog;
import com.yantong.pojo.CardConfig;
import com.yantong.service.ConfigService;
import com.yantong.pojo.Member;
import com.yantong.mapper.MemberMapper;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Administrator on 2020-11-03.
 */
@Service
public class TBusinessCardServiceImpl implements TBusinessCardService {

    @Autowired
    private TBusinessCardMapper tBusinessCardMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ConfigService configService;
    @Autowired
    private BusinessCardIndustryMapper industryMapper;

    @Override
    public List<TBusinessCard> searchMyFriends(Long tCardId, String wxCodeOrPhone) {
        return tBusinessCardMapper.searchMyFriends(tCardId, wxCodeOrPhone);
    }

    @Override
    public List<TBusinessCard> myFriends(Long tCardId,String industryId) {
        return tBusinessCardMapper.myFriends(tCardId,industryId);
    }

    @Override
    public TBusinessCard selectCardByPhone(String phone) {
        return tBusinessCardMapper.selectCardByPhone(phone);
    }

    /**
     * 查询电子名片信息
     *
     * @param phone 手机号码
     * @return 电子名片信息
     */
    public TBusinessCard selectTBusinessCardByPhone(Long tCardId, String phone) {
        return tBusinessCardMapper.selectTBusinessCardByPhone(tCardId, phone);
    }

    ;

    /**
     * 查询电子名片信息
     *
     * @param id 电子名片信息ID
     * @return 电子名片信息
     */
    @Override
    public TBusinessCard selectTBusinessCardById(Long id) {
        return tBusinessCardMapper.selectTBusinessCardById(id);
    }

    /**
     * 查询电子名片信息列表
     *
     * @param tBusinessCard 电子名片信息
     * @return 电子名片信息
     */
    @Override
    public List<TBusinessCard> selectTBusinessCardList(TBusinessCard tBusinessCard) {
        return tBusinessCardMapper.selectTBusinessCardList(tBusinessCard);
    }

    /**
     * 新增电子名片信息
     *
     * @param tBusinessCard 电子名片信息
     * @return 结果
     */
    @Override
    public int insertTBusinessCard(TBusinessCard tBusinessCard) {
        Random random = new Random();
        //生成600-10000内的随机数
        int s = random.nextInt(10000) % (10000 - 602 + 1) + 602;
        tBusinessCard.setGzNum(s);
        return tBusinessCardMapper.insertTBusinessCard(tBusinessCard);
    }


    /**
     * 修改电子名片信息
     *
     * @param tBusinessCard 电子名片信息
     * @return 结果
     */
    @Override
    public int updateTBusinessCard(TBusinessCard tBusinessCard) {
        Random random = new Random();
        //生成600-10000内的随机数
        int s = random.nextInt(10000) % (10000 - 602 + 1) + 602;
        tBusinessCard.setGzNum(s);
        return tBusinessCardMapper.updateTBusinessCard(tBusinessCard);
    }

    /**
     * 删除电子名片信息信息
     *
     * @param id 电子名片信息ID
     * @return 结果
     */
    public int deleteTBusinessCardById(Long id) {
        return tBusinessCardMapper.deleteTBusinessCardById(id);
    }

    @Override
    public PageUtils<BrokerageVo> brokerageList(int offset, int limit) {
        Map params = new HashMap();
//        if (StringUtils.isNotEmpty(title)) {
//            params.put("title", title);
//        }
//        if (StringUtils.isNotEmpty(startTime)) {
//            params.put("startTime", startTime);
//        }
//        if (StringUtils.isNotEmpty(endTime)) {
//            params.put("endTime", endTime);
//        }
        PageUtils<BrokerageVo> pageUtils = new PageUtils<>();
        int total = tBusinessCardMapper.countBybrokerageListt(params);
        if (total > 0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(tBusinessCardMapper.brokerageList(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public BrokerageVo getBrokerageById(Long id) {
        return tBusinessCardMapper.getBrokerageById(id);
    }


    @Override
    public PageUtils<CommissionLog> commissionLogList(String mid, String type, String thisMonth, Integer offset, Integer limit) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(mid)) {
            params.put("mid", mid);
        }
        if (StringUtils.isNotEmpty(type)) {
            params.put("type", type);
        }
        if (StringUtils.isNotEmpty(thisMonth)) {
            params.put("thisMonth", thisMonth);
        }
        PageUtils<CommissionLog> pageUtils = new PageUtils<>();
        int total = tBusinessCardMapper.countByCommissionLogList(params);
        if (total > 0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(tBusinessCardMapper.commissionLogList(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public void editBrokerage(Brokerage brokerage) {
        tBusinessCardMapper.editBrokerage(brokerage);
    }


    @Override
    public List<BusinessCardIndustry> getIndustryList() {
        return industryMapper.selectByExample(new BusinessCardIndustryExample());
    }

    @Override
    public List<TBusinessCard> selectByAll() {
        return tBusinessCardMapper.selectByAll();
    }

}
